package com.bajiao.common.util;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author FanJq
 * @description 公共工具类
 * @date 2024-10-10
 */
public class CommonUtil {


    /**
     * 获取折扣金额
     *
     * @param price    金额
     * @param discount 折扣 例：7=7折
     * @return
     */
    public static double getDiscountPrice(Double price, Double discount) {
        if (discount <= 0) {
            //0折视为不打折
            return 0;
        }
        BigDecimal price1 = new BigDecimal(Double.toString(price));
        BigDecimal price2 = new BigDecimal(Double.toString(price));
        BigDecimal dis = new BigDecimal(Double.toString(discount));
        return price1.subtract(price2.multiply(dis.multiply(new BigDecimal("0.1"))).setScale(2, BigDecimal.ROUND_DOWN))
                .max(BigDecimal.ZERO).doubleValue();
    }

    /**
     * 数字格式化 例：1111 ->1.1k 11111 ->1.1w
     *
     * @param number
     * @return
     */
    public static String numberFormat(Integer number) {
        if (number == null) {
            return "0";
        }
        BigDecimal num = new BigDecimal(number);
        int wan = 10000;
        int qian = 1000;
        BigDecimal wanD = new BigDecimal(wan);
        BigDecimal qianD = new BigDecimal(qian);
        if (number >= wan) {
            return num.divide(wanD, 1, BigDecimal.ROUND_HALF_UP).toString()+"w";
        }
        if (number >= qian) {
            return num.divide(qianD, 1, BigDecimal.ROUND_HALF_UP).toString()+"k";
        }
        return number.toString();
    }

    /**
     * 日期格式化 1分钟前 1小时前等等
     *
     * @param date
     * @return
     */
    public static String dateFormat(Date date) {
        Date now = new Date();
        long day = DateUtil.between(date, now, DateUnit.DAY);
        if (day >= 1) {
            return DateUtil.formatDateTime(date);
        }
        long hour = DateUtil.between(date, now, DateUnit.HOUR);
        if (hour >= 1) {
            return hour+"小时前";
        }
        long minute = DateUtil.between(date, now, DateUnit.MINUTE);
        if (minute >= 1) {
            return minute+"分钟前";
        } else {
            return "刚刚";
        }
    }

    /**
     * 距离格式化 例：99转<100m 100转100m 10000转1km
     *
     * @param distance
     * @return
     */
    public static String distanceFormat(Integer distance) {
        if (distance >= 1000) {
            return new BigDecimal(distance)
                    .divide(new BigDecimal("1000"), 1, BigDecimal.ROUND_HALF_UP).toString()+"km";
        } else if (distance < 100) {
            return "<100m";
        } else {
            return distance+"m";
        }
    }

    /**
     * 匿名格式化 例 wow转w*w wo转w*
     *
     * @param name
     * @param pattern 格式 “*”
     * @return
     */
    public static String anonymousFormat(String name, String pattern) {
        StringBuilder sb = new StringBuilder(name);
        if (name.length() > 2) {
            sb.replace(1, name.length()-1, pattern);
        } else {
            sb.replace(name.length()-1, name.length(), pattern);
        }
        return sb.toString();
    }
}
